package jdk8.stream;

import java.util.ArrayList;
import java.util.stream.Stream;

/**
 * Steam常用方法
 **/
public class Demo4CommonMethod {
    public static void main(String[] args) {
        // 遍历
        Stream.of('a', 'b').forEach(System.out::println);
        // 过滤
        Stream<String> stream =  Stream.of("张三丰","张无忌","赵敏");
        Stream<String> filterStream = stream.filter((String s) -> {return s.startsWith("张");});
        filterStream.forEach(System.out::println);

        // Stream 是管道流
//        java.lang.IllegalStateException: stream has already been operated upon or closed
//        stream.forEach(System.out::println);

        // 映射方法
        Stream<String> stream1 = Stream.of("12", "33", "555");
        Stream<Integer> streamMap = stream1.map((String s)-> {
            return Integer.parseInt(s);
        } );

        streamMap.forEach(integer -> System.out.println(integer.getClass()));

        // 统计元素(终结方法)
        ArrayList<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(23);
        list.add(343);

        System.out.println("统计 > " + list.stream().count());

        // 截取Stream
        Stream<String> stream2 = Stream.of("美羊羊","喜羊羊", "灰太狼", "香蕉狼");
        Stream<String> stream3 = stream2.limit(3);
        System.out.println("获取前三个: ");
        stream3.forEach(System.out::println);

        //跳过元素
        Stream<String> stream4 = Stream.of("美羊羊","喜羊羊", "灰太狼", "香蕉狼");
        Stream<String> stream5 = stream4.skip(2);
        System.out.println("跳过前两个: ");
        stream5.forEach(System.out::println);

        // 组合流
        Stream<String> stream6 = Stream.of("赵钱孙李");
        Stream<String> stream7 = Stream.of("周吴郑王");
        Stream<String> stream8 = Stream.concat(stream6, stream7);
        System.out.println("组合流: ");
        stream8.forEach(System.out::println);

    }
}
